<?php

/**
 * This file is part of the Codizer package.
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 *
 * @copyright   Copyright (c) 2010 Advisa (http://www.advisa.fr)
 * @author      Codizer Core Team <team@codizer.org>
 * @package     Codizer
 */

class f_utils_String
{
	/**
	 * Return $string without accents
	 *
	 * @param string $string
	 * @return string
	 */
	public static function strip_accents($string)
	{
		$a = "ÀÁÂÃÄÅàáâãäåÒÓÔÕÖØòóôõöøÈÉÊËèéêëÇçÌÍÎÏìíîïÙÚÛÜùúûüÿÑñ";
		$b = "AAAAAAaaaaaaOOOOOOooooooEEEEeeeeCcIIIIiiiiUUUUuuuuyNn";
		$string = utf8_decode($string);
		return(strtr($string, utf8_decode($a), $b));
	}

    /**
     * UTF-8 compliant substr
     *
     * @param string $str
     * @param integer $from
     * @param integer $len
     * @return string
     */
    public static function substru($str,$from,$len)
    {
        return preg_replace('#^(?:[\x00-\x7F]|[\xC0-\xFF][\x80-\xBF]+){0,'. $from .'}'.'((?:[\x00-\x7F]|[\xC0-\xFF][\x80-\xBF]+){0,'. $len .'}).*#s','$1', $str);
    }

    /**
     * Truncate a string at a precise length
     *
     * @param string $string
     * @param integer $length
     * @param string $pad
     * @return string
     */
    public static function truncateAtLength($string, $length, $pad = '...')
    {
        // string is shorter, return string
        if(strlen($string) <= $length) return $string;

        return self::substru($string, 0, $length).$pad;
    }

    /**
     * Truncate a string at sentence breaks
     *
     * @param string $string
     * @param integer $length
     * @param string $pad
     * @return string
     */
    public static function truncateAtSentence($string, $length, $pad = '.')
    {
         if(strlen($string) <= $length) return $string;

         if(false !== ($breakpoint = strpos(utf8_decode($string), '.', $length)))
        {
            if($breakpoint < strlen(utf8_decode($string)) - 1)
            {
                return self::substru($string, 0, $breakpoint).$pad;
            }
        }
        return $string;
    }

    /**
     * Truncate a string at word breaks
     *
     * @param string $string
     * @param integer $length
     * @param string $pad
     * @return string
     */
    public static function truncateAtWord($string, $length, $pad = '...')
    {
         if(strlen($string) <= $length) return $string;

         if(false !== ($breakpoint = strpos(utf8_decode($string), ' ', $length)))
        {
            if($breakpoint < strlen(utf8_decode($string)) - 1)
            {
                return self::substru($string, 0, $breakpoint).$pad;
            }
        }
        return $string;
    }

	/**
	 * Escape word special chars when copy-paste from MS word
	 *
	 * @param unknown_type $text
	 * @return unknown
	 */
	public static function escapeWordsChars($text)
	{
		$badwordchars = array(chr(128), chr(133), chr(145), chr(146), chr(147), chr(148), chr(149), chr(150), chr(151), chr(153), chr(169), chr(174));
		$fixedwordchars = array('&euro;', '...', "'", "'", '"', '"', '&bull;', '&ndash;', '&mdash;', '&trade;','&copy;', '&reg');

		return str_replace($badwordchars, $fixedwordchars, $text);
	}

	/**
	 * Check if $needle string is in $haystack string
	 *
	 * @param string $needle
	 * @param string $haystack
	 * @return boolean
	 */
	public static function isInString($needle, $haystack)
	{
		if(strstr($haystack, $needle) == false)
		{
			return false;
		}
		return true;
	}

	/**
	 * Equivalent to lcfirst
	 *
	 * @param string $string
	 * @return string
	 */
	public static function framework_lcfirst($string)
	{
		return strtolower($string{0}) . substr($string, 1);
	}

	/**
	 * To use before PHP 5.3
	 * @param $haystack
	 * @param $needle
	 * @return unknown_type
	 */
	public static function rstrstr($haystack, $needle)
	{
	    return substr(strrev(strstr(strrev($haystack), strrev($needle))), 0, -strlen($needle));
	}

	/**
	 * Generate a random string
	 * @return string
	 */
	public static function generateString($length = 7, $specialChars = true)
	{
		$chars='abcdefghijklmnopqrstuvwyxz0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ';

        if($specialChars == true)
        {
            $chars .= '@_';
        }

		$pass='';
		$l=strlen($chars)-1;

		for($i=0;$i<$length;$i++)
		{
			$index=rand(0,$l);
			$pass.=$chars[$index];
		}

		return $pass;
	}

    /**
     * Return image src from img tags in a text
     * @param string $content
     * @return array
     */
    public static function getImgSrcFromTags($content)
    {
        preg_match_all('/< *img[^>]*src *= *["\']?([^"\']*)/i', $content, $patterns);

        return $patterns[1];
    }
}